package it.univpm.diiga.memorysim.kernel.shared.tree;

import java.util.ArrayList;
import java.util.List;

public class MovingBox extends Model {   //questa � una directory
	public int dimensione=0;
	
	protected List<MovingBox> boxes;
	
	private static IModelVisitor adder = new Adder();
	private static IModelVisitor remover = new Remover();
	
	public MovingBox() {
		boxes = new ArrayList<MovingBox>();
	}
	
	private static class Adder implements IModelVisitor {

		public void visitNode(File node, Object argument) {
			((MovingBox) argument).addFile(node);
		}

		/*
		 * @see ModelVisitorI#visitMovingBox(MovingBox, Object)
		 */
		public void visitMovingBox(MovingBox box, Object argument) {
			((MovingBox) argument).addBox(box);
		}

	}

	private static class Remover implements IModelVisitor {

		public void visitNode(File node, Object argument) {
			((MovingBox) argument).removeFile(node);
		}

		/*
		 * @see ModelVisitorI#visitMovingBox(MovingBox, Object)
		 */
		public void visitMovingBox(MovingBox box, Object argument) {
			((MovingBox) argument).removeBox(box);
			box.addListener(NullDeltaListener.getSoleInstance());
		}

	}
	
	public MovingBox(String name) {
		this();
		this.name = name;
	}
	
	public List<MovingBox> getBoxes() {
		return boxes;
	}
	
	public void addBox(MovingBox box) {
		boxes.add(box);
		box.parent = this;
		fireAdd(box);
	}
	
	public void addFile(File node) {
		node.parent = this;
		fireAdd(node);
	}
	

	public void remove(Model toRemove) {
		toRemove.accept(remover, this);
	}
	
	protected void removeFile(File node) {
		node.addListener(NullDeltaListener.getSoleInstance());
		fireRemove(node);
	}
	
	protected void removeBox(MovingBox box) {
		boxes.remove(box);
		box.addListener(NullDeltaListener.getSoleInstance());
		fireRemove(box);	
	}

	public void add(Model toAdd) {
		toAdd.accept(adder, this);
	}
	

	public int size() {
		return getBoxes().size() ;
	}
	/*
	 * @see Model#accept(ModelVisitorI, Object)
	 */
	public void accept(IModelVisitor visitor, Object passAlongArgument) {
		visitor.visitMovingBox(this, passAlongArgument);
	}

}
